package com.wj.facedetect.activity.alarm;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.facebook.drawee.view.SimpleDraweeView;
import com.wj.facedetect.R;
import com.wj.facedetect.activity.base.BaseActivity;
import com.wj.facedetect.bean.alarm.MyTaskBean;
import com.wj.facedetect.net.ReqUrls;
import com.wj.facedetect.presenter.AlarmPresenter;
import com.wj.facedetect.util.PreferencesUtils;
import com.wj.facedetect.util.TimeUtils;
import com.wj.facedetect.view.IBaseView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyTaskActivity extends BaseActivity implements IBaseView, CanRefreshLayout.OnLoadMoreListener, CanRefreshLayout.OnRefreshListener {
    private TextView tvLeft;
    private CanRefreshLayout canRefreshLayout;
    private RecyclerView recyclerView;
    private AlarmPresenter alarmPresenter;
    private int pageNo = 1, pageSize = 5;
    private List<MyTaskBean.DataBean.ListBean> list = new ArrayList<>();
    private MyTaskAdapter adapter;
    private static final int REQ_GET_MY_TASK = 0x001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_task);
        alarmPresenter = new AlarmPresenter(this);
        initView();
    }

    private void initView() {
        canRefreshLayout = (CanRefreshLayout) findViewById(R.id.refresh);
        recyclerView = (RecyclerView) findViewById(R.id.can_content_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new MyTaskAdapter();
        recyclerView.setAdapter(adapter);
        canRefreshLayout.setOnLoadMoreListener(this);
        canRefreshLayout.setOnRefreshListener(this);
        canRefreshLayout.autoRefresh();
        tvLeft = (TextView) findViewById(R.id.tv_left);
        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    public void requestData() {
        HashMap<String, String> params = new HashMap<>();
        params.put("user", PreferencesUtils.getInstance().getSharePreInt(PreferencesUtils.USER_ID) + "");
        params.put("pageNo", pageNo + "");
        params.put("pageSize", pageSize + "");
        alarmPresenter.doRequestPostTaskMyTask(params, REQ_GET_MY_TASK, this);
    }


    @Override
    public void onLoadMore() {
        pageNo = pageNo + 1;
        canRefreshLayout.setRefreshEnabled(false);
        requestData();
    }

    @Override
    public void onRefresh() {
        pageNo = 1;
        canRefreshLayout.setLoadMoreEnabled(false);
        requestData();
    }

    @Override
    public void onSuccess(int what, Object data, Object carryObj) {
        if (canRefreshLayout != null) {
            canRefreshLayout.setLoadMoreEnabled(true);
            canRefreshLayout.setRefreshEnabled(true);
            canRefreshLayout.refreshComplete();
            canRefreshLayout.loadMoreComplete();
        }
        if (what == REQ_GET_MY_TASK) {
            MyTaskBean myTaskBean = (MyTaskBean) data;
            if (myTaskBean != null) {
                if (myTaskBean.isSuccess()) {
                    MyTaskBean.DataBean dataBean = myTaskBean.getData();
                    if (pageNo == 1) {
                        list = dataBean.getList();
                    } else {
                        list.addAll(dataBean.getList());
                    }
                    adapter.notifyDataSetChanged();
                }
            }
        }
    }

    @Override
    public void onError(int what, Exception e, Object carryObj) {
        if (canRefreshLayout != null) {
            canRefreshLayout.setLoadMoreEnabled(true);
            canRefreshLayout.setRefreshEnabled(true);
            canRefreshLayout.refreshComplete();
            canRefreshLayout.loadMoreComplete();
        }
    }

    @Override
    public void onFail(int what, int errorCode, String errorMsg, Object carryObj) {
        if (canRefreshLayout != null) {
            canRefreshLayout.setLoadMoreEnabled(true);
            canRefreshLayout.setRefreshEnabled(true);
            canRefreshLayout.refreshComplete();
            canRefreshLayout.loadMoreComplete();
        }
    }

    class MyTaskAdapter extends RecyclerView.Adapter<MyTaskAdapter.MyViewHolder> {
        @Override
        public MyTaskAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(MyTaskActivity.this).inflate(R.layout.alarm_item, parent, false);
            return new MyTaskAdapter.MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MyTaskAdapter.MyViewHolder holder, int position) {
            final MyTaskBean.DataBean.ListBean listBean = list.get(position);
            if (!TextUtils.isEmpty(listBean.getAcquiredFaceImg())) {
                String photoUrl1Temp = listBean.getAcquiredFaceImg().replace("\n", "");
                holder.headView.setImageURI(PreferencesUtils.getInstance().getSharePreStrBaseUrl(PreferencesUtils.BASE_URL)
                        + String.format(ReqUrls.URL_GET_PHOTO2, listBean.getClusterId(), photoUrl1Temp));
            }
            if (!TextUtils.isEmpty(listBean.getPersonName())) {
                holder.tvXyfName.setText(listBean.getPersonName());
            }
            if (!TextUtils.isEmpty(listBean.getAddr())) {
                holder.tvAddr.setText(listBean.getAddr());
            }
            BigDecimal b = listBean.getHitPrecent();
            if (b != null) {
                double ppd = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                holder.tvSim.setText(ppd + "");
            }
            long time = listBean.getTime();
            long timeLong = time * 1000;
            String formatTimeByType = TimeUtils.getFormatTimeByType(timeLong, TimeUtils.DATE_FORMAT_MMDDHHMM);
            holder.tvTime.setText(formatTimeByType);
            holder.tvKuName.setText(listBean.getRepositoryName());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(MyTaskActivity.this, AlarmDetailActivity.class);
                    intent.putExtra("message", listBean.getId() + "");
                    startActivityForResult(intent, 110);
                }
            });
            holder.tvStatus.setVisibility(View.VISIBLE);
            if ("IDENTIFICATION_TASK".equals(listBean.getTaskStatus())) {
                holder.tvStatus.setText("处理中");
                holder.tvStatus.setTextColor(getResources().getColor(R.color.medium_text_enabled));
            } else if ("TASK_ACCOMPLISHED".equals(listBean.getTaskStatus())) {
                holder.tvStatus.setText("已处理");
                holder.tvStatus.setTextColor(getResources().getColor(R.color.green));
            }
        }

        @Override
        public int getItemCount() {
            return list == null ? 0 : list.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            private SimpleDraweeView headView;
            private TextView tvKuName, tvSim, tvAddr, tvXyfName, tvTime, tvStatus;

            public MyViewHolder(View itemView) {
                super(itemView);
                headView = itemView.findViewById(R.id.iv_head_1);
                tvKuName = itemView.findViewById(R.id.tv_ku_name);
                tvSim = itemView.findViewById(R.id.tv_sim);
                tvAddr = itemView.findViewById(R.id.tv_addr);
                tvXyfName = itemView.findViewById(R.id.tv_xyf_name);
                tvTime = itemView.findViewById(R.id.tv_time);
                tvStatus = itemView.findViewById(R.id.tv_status);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 110 && RESULT_OK == resultCode) {
            if (canRefreshLayout != null) {
                onRefresh();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
